package dice.sinanya.start;

import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;

import static dice.sinanya.SinaNya.systemType;

public interface FindDir {

    File findDir(String fileName) throws IOException;

    default File findMyDir() throws IOException {
        switch (systemType) {
            case MAC:
            case LINUX:
                String path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
                File jarDir = new File(URLDecoder.decode(path, "UTF-8"));
                if (jarDir.exists()) {
                    File installDir = jarDir.getParentFile().getParentFile();
                    if (installDir.exists() && installDir.isDirectory()) {
                        return installDir;
                    } else {
                        path = "installDir:\t" + installDir.getCanonicalPath();
                    }
                } else {
                    path = "path:\t" + path;
                }
                throw new IOException("查找安装目录失败:\t" + path);
            case WIN:
                path = new File("").getAbsolutePath();
                jarDir = new File(URLDecoder.decode(path, "UTF-8"));
                if (jarDir.exists()) {
                    if (jarDir.exists() && jarDir.isDirectory()) {
                        return jarDir;
                    }
                }
                throw new IOException("查找安装目录失败:\t" + path);
            default:
                throw new IOException("系统检测失败");
        }

    }

}
